package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.SysCity;

public class SysCityRowMapper implements RowMapper<SysCity> {

    @SuppressWarnings("unused")
    private int type;

    public SysCityRowMapper() {
    }

    public SysCityRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SysCity mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseSysCity(new SysCity(), rs);
    }

    public static SysCity parseSysCity(SysCity sc, ResultSet rs)
        throws SQLException {
        return parseSysCity(sc, rs, null);
    }

    public static SysCity parseSysCity(SysCity sc, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            sc.setCid(rs.getInt(su + "cid"));
        } catch (SQLException e) {
            sc.setCid(null);
        }
        try {
            sc.setName(rs.getString(su + "name"));
        } catch (SQLException e) {
            sc.setName(null);
        }

        try {
            sc.setSubCid(rs.getInt(su + "sub_cid"));
        } catch (SQLException e) {
            sc.setSubCid(null);
        }

        try {
            sc.setCtype(rs.getShort(su + "ctype"));
        } catch (SQLException e) {
            sc.setCtype(null);
        }

        try {
            sc.setPinJ(rs.getString(su + "pin_j"));
        } catch (SQLException e) {
            sc.setPinJ(null);
        }

        try {
            sc.setPinA(rs.getString(su + "pin_a"));
        } catch (SQLException e) {
            sc.setPinA(null);
        }

        try {
            sc.setPinAS(rs.getString(su + "pin_as"));
        } catch (SQLException e) {
            sc.setPinA(null);
        }

        try {
            sc.setCstate(rs.getShort(su + "cstate"));
        } catch (SQLException e) {
            sc.setCstate(null);
        }

        return sc;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SysCity sc) {
        par.put("name", sc.getName());
        par.put("sub_cid", sc.getSubCid());
        par.put("ctype", sc.getCtype());
        par.put("pin_j", sc.getPinJ());
        par.put("pin_a", sc.getPinA());
        par.put("pin_as", sc.getPinAS());
        par.put("cstate", sc.getCstate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            SysCity sc) {
        args.put("cid", sc.getCid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SysCity sc) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(sc),
                        new String[] { "cid" });
                parsePS(ps, sc);
                return ps;
            }

        };
    }

    private static String parseSQL(SysCity sc) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into sys_city(");
        int count = 0;

        if (null != sc.getName()) {
            sql.append("name,");
            count++;
        }

        if (null != sc.getSubCid()) {
            sql.append("sub_cid,");
            count++;
        }

        if (null != sc.getCtype()) {
            sql.append("ctype,");
            count++;
        }
        if (null != sc.getPinJ()) {
            sql.append("pin_j,");
            count++;
        }
        if (null != sc.getPinA()) {
            sql.append("pin_a,");
            count++;
        }
        if (null != sc.getPinAS()) {
            sql.append("pin_as,");
            count++;
        }
        if (null != sc.getCstate()) {
            sql.append("cstate,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SysCity sc)
        throws SQLException {
        int count = 1;

        if (null != sc.getName()) {
            ps.setString(count++, sc.getName());
        }

        if (null != sc.getSubCid()) {
            ps.setInt(count++, sc.getSubCid());
        }

        if (null != sc.getCtype()) {
            ps.setShort(count++, sc.getCtype());
        }
        if (null != sc.getPinJ()) {
            ps.setString(count++, sc.getPinJ());
        }
        if (null != sc.getPinA()) {
            ps.setString(count++, sc.getPinA());
        }
        if (null != sc.getPinAS()) {
            ps.setString(count++, sc.getPinAS());
        }
        if (null != sc.getCstate()) {
            ps.setShort(count++, sc.getCstate());
        }
    }
}
